<?php
class EventModule
{
	public function index()
	{
		global $_FANWE;

			//我的旅途
			$my_list = array();
			$noew = time();
			$my_list = FDB::fetchAll('SELECT a.*, (select count(id) from '.FDB::table("activities_relation").' where aid=a.id ) count 
								FROM '.FDB::table("activities").' as a where a.uid = '.$_FANWE['uid'].'  ORDER BY time_start DESC limit 10');
			for( $i=0; $i<count($my_list); $i++ ){
				$act_temp = unserialize($my_list[$i]['pics'] );
				$my_list[$i]['pics'] = $act_temp[0];
				$info_temp = unserialize($my_list[$i]['info'] );
				$my_list[$i]['info'] = $info_temp;
			}
			
			//我参加的旅途
			$my_join = array();
			$my_join_list  =  FDB::fetchAll('select * from '.FDB::table("activities").' where id in (select aid from '.FDB::table("activities_relation").'  where uid='.$_FANWE['uid'].' )
							order by time_start desc');
							
			for( $i=0; $i<count($my_join_list); $i++ ){
				$act_temp = unserialize($my_join_list[$i]['pics'] );
				$my_join_list[$i]['pics'] = $act_temp[0];
				$info_temp = unserialize($my_join_list[$i]['info'] );
				$my_join_list[$i]['info'] = $info_temp;
			}

			
			
			//最新活动
			$acti_new_list = array();
			$acti_new_list = FDB::fetchAll('SELECT a.*, (select count(id) from '.FDB::table("activities_relation").' where aid=a.id ) count 
								FROM '.FDB::table("activities").' as a  ORDER BY time_start DESC');
			for( $i=0; $i<count($acti_new_list); $i++ ){
				$act_temp = unserialize($acti_new_list[$i]['pics'] );
				$acti_new_list[$i]['pics'] = $act_temp[0];
				$info_temp = unserialize($acti_new_list[$i]['info'] );
				$acti_new_list[$i]['info'] = $info_temp;
			}
			
			//其它活动
			$acti_list = array();
			$acti_list = FDB::fetchAll('SELECT a.*, (select count(id) from '.FDB::table("activities_relation").' where aid=a.id ) count 
								FROM '.FDB::table("activities").' as a  ORDER BY count DESC limit 10');
			for( $i=0; $i<count($acti_list); $i++ ){
				$act_temp = unserialize($acti_list[$i]['pics'] );
				$acti_list[$i]['pics'] = $act_temp[0];
				$info_temp = unserialize($acti_list[$i]['info'] );
				$acti_list[$i]['info'] = $info_temp;
			}

		
		include template('page/event/event_list');
		display();		
	}
	
	public function detail()
	{
		global $_FANWE;
		$id = intval($_FANWE['request']['id']);
		if($id == 0){
			fHeader("location: ".FU('index/index'));
		}
		$event_sql = "select a.*,b.uid,b.user_name from  ".FDB::table('activities')." as a
						left join ".FDB::table('user')." as b 
						on a.uid=b.uid   where a.id = ".$id;
		$event_detail = FDB::fetchFirst($event_sql);
		$event_detail['avatar'] = avatar($event_detail['uid'], 'm', $is_src = 1);
		$event_detail['pics'] = unserialize( $event_detail['pics'] );
		$event_detail_info = unserialize( $event_detail['info'] );//活动详情
		
		$event_detail['members'] = FDB::fetchAll("select a.uid,a.info,b.user_name from  ".FDB::table('activities_relation')." as a
							left join ".FDB::table('user')." as b 
							on a.uid=b.uid where a.aid = ".$id);
		$event_detail['mem_count'] = count($event_detail['members']);
		$event_detail['now'] = time();
			//评论
			$comments_list = array();
			$comments_list = FDB::fetchAll('SELECT a.*,b.user_name 
						FROM '.FDB::table("activities_comment").' as a left join  '.FDB::table("user").' as b 
						on a.uid=b.uid WHERE a.share_id = '.$id.'
						ORDER BY a.comment_id DESC');
			$event_detail['comments'] = $comments_list;
			
		//其它活动
		$acti_list = array();
		$acti_list = FDB::fetchAll('SELECT a.*, (select count(id) from '.FDB::table("activities_relation").' where aid=a.id ) count 
							FROM '.FDB::table("activities").' as a
							WHERE id<>'.$id.' ORDER BY count DESC limit 10');
		for( $i=0; $i<count($acti_list); $i++ ){
			$act_temp = unserialize($acti_list[$i]['pics'] );
			$acti_list[$i]['pics'] = $act_temp[0];
		}
		$event_detail['acti_list'] = $acti_list;
		
		include template('page/event/event_detail');
		display();		
	}
	
	public function listdetail()
	{
		global $_FANWE;
		if(isset($_FANWE['request']['type'])){
			$type = $_FANWE['request']['type'];
		}
		$action = $_FANWE['request']['order'];
		$id = $_POST['id'];
		$content =  $_POST['content'];
		if( $type == "ajax" ){
			switch($action){
				case "guanzhu":
					if( $_FANWE['uid'] == 0 ){
						$json_data = array("msg"=>"notlogin");
					}else{
						$sql = "update ".FDB::table("activities")." set fav=fav+1 where id={$id}";
						$query = FDB::query($sql);
						if($query){
							$json_data = array("msg"=>"success");
						}else{
							$json_data = array("msg"=>"error");
						}
					}
					break;
				case "lvtulequ":
					$sql = "insert into ".FDB::table("activities_comment")."  (share_id, uid, content, create_time)
								values(".$id.", ".$_FANWE['uid'].", '{$content}', ".time().")";
					$query = FDB::query($sql);
					if($query){
						$json_data = array("msg"=>"success");
					}else{
						$json_data = array("msg"=>"error");
					}
					break;
				case "savenew":
					if( $_FANWE['uid'] == 0 ){
						$json_data = array("msg"=>"notlogin");
					}else{
						$post_info = array(
							'content' => $_POST['detail'],
							'name'	  => $_POST['name'],
							'tel'	  => $_POST['tel'],
							'add'	  => $_POST['add'],
						);
						$post_info = serialize($post_info);
						$pics = explode("|",trim($_POST['pics'],"|"));
						$post_pics = serialize($pics);
						$sql = "insert into ".FDB::table("activities")."  (uid, title, info, pics, time_start, time_end, fav)
									values(".$_FANWE['uid'].", '".$_POST['title']."', '{$post_info}', '{$post_pics}', ".strtotime($_POST['t_start']).", ".strtotime($_POST['t_end']).", 10)";
						$query = FDB::query($sql);
						if($query){
							$json_data = array("msg"=>"success");
						}else{
							$json_data = array("msg"=>"error");
						}
					}
					break;
				case "delcomm":
					$sql = "delete from ".FDB::table("activities_comment")."  where comment_id={$id}";
					$query = FDB::query($sql);
					if($query){
						$json_data = array("msg"=>"success");
					}else{
						$json_data = array("msg"=>"error");
					}
					break;
				case "join":
					if( $_FANWE['uid'] == 0 ){
						$json_data = array("msg"=>"notlogin");
					}else{
						$sqla = "select count(id) from ".FDB::table("activities_relation")." where aid=".$id." and uid=".$_FANWE['uid'] ;
						$count = FDB::resultFirst($sqla);
						if( $count>0 ){
							$json_data = array("msg"=>"exist");
						}else{
							$info = serialize(array(
								"tel"  => $_POST['tel'],
								"name" => $_POST['name']
							));
							$sql = "insert into ".FDB::table("activities_relation")."  (aid, uid, info)
										values(".$id.", ".$_FANWE['uid'].", '{$info}')";
							$query = FDB::query($sql);
							if($query){
								$json_data = array("msg"=>"success");
							}else{
								$json_data = array("msg"=>"error");
							}					
						}
					}
					break;
			}
			echo json_encode( $json_data );
		}elseif( $type=="addnew" ){
			session_start();
			if( $_FANWE['uid'] != 0 ){
				$_SESSION['isuserlogin'] = true;
			}
			
			
			//我的活动
			$my_list = array();
			$my_list = FDB::fetchAll('SELECT a.*, (select count(id) from '.FDB::table("activities_relation").' where aid=a.id ) count 
								FROM '.FDB::table("activities").' as a where a.uid = '.$_FANWE['uid'].'  ORDER BY time_start DESC limit 10');
			for( $i=0; $i<count($my_list); $i++ ){
				$act_temp = unserialize($my_list[$i]['pics'] );
				$my_list[$i]['pics'] = $act_temp[0];
			}
			$event_detail['my_list'] = $my_list;
			
			//其它活动
			$acti_list = array();
			$acti_list = FDB::fetchAll('SELECT a.*, (select count(id) from '.FDB::table("activities_relation").' where aid=a.id ) count 
								FROM '.FDB::table("activities").' as a  ORDER BY count DESC limit 10');
			for( $i=0; $i<count($acti_list); $i++ ){
				$act_temp = unserialize($acti_list[$i]['pics'] );
				$acti_list[$i]['pics'] = $act_temp[0];
			}
			$event_detail['acti_list'] = $acti_list;

			include template('page/event/event_add');
			display();

		}elseif( $type=="mine" ){
		
			if( $_FANWE['uid'] == 0 ){
				die('<script>alert("您还没有登陆！请先登陆！"); location.href="'. FU('user/login') .'"</script>');
			}
			
			//我的旅途
			$my_list = array();
			$noew = time();
			$my_list = FDB::fetchAll('SELECT a.*, (select count(id) from '.FDB::table("activities_relation").' where aid=a.id ) count 
								FROM '.FDB::table("activities").' as a where a.uid = '.$_FANWE['uid'].'  ORDER BY time_start DESC limit 10');
			for( $i=0; $i<count($my_list); $i++ ){
				$act_temp = unserialize($my_list[$i]['pics'] );
				$my_list[$i]['pics'] = $act_temp[0];
				$info_temp = unserialize($my_list[$i]['info'] );
				$my_list[$i]['info'] = $info_temp;
			}

			
			
			//其它活动
			$acti_list = array();
			$acti_list = FDB::fetchAll('SELECT a.*, (select count(id) from '.FDB::table("activities_relation").' where aid=a.id ) count 
								FROM '.FDB::table("activities").' as a  ORDER BY count DESC limit 10');
			for( $i=0; $i<count($acti_list); $i++ ){
				$act_temp = unserialize($acti_list[$i]['pics'] );
				$acti_list[$i]['pics'] = $act_temp[0];
				$info_temp = unserialize($acti_list[$i]['info'] );
				$acti_list[$i]['info'] = $info_temp;
			}

			
			
			
			include template('page/event/event_list_mine');
			display();
		
		}elseif( $type=="myjoin" ){

			
			if( $_FANWE['uid'] == 0 ){
				die('<script>alert("您还没有登陆！请先登陆！"); location.href="'. FU('user/login') .'"</script>');
			}
			
			//我参加的旅途
			$my_join = array();
			$my_join_list  =  FDB::fetchAll('select * from '.FDB::table("activities").' where id in (select aid from '.FDB::table("activities_relation").'  where uid='.$_FANWE['uid'].' )
							order by time_start desc');
							
			for( $i=0; $i<count($my_join_list); $i++ ){
				$act_temp = unserialize($my_join_list[$i]['pics'] );
				$my_join_list[$i]['pics'] = $act_temp[0];
				$info_temp = unserialize($my_join_list[$i]['info'] );
				$my_join_list[$i]['info'] = $info_temp;
			}

			$my_list = $my_join_list;
			
			
			
			
			
			//其它活动
			$acti_list = array();
			$acti_list = FDB::fetchAll('SELECT a.*, (select count(id) from '.FDB::table("activities_relation").' where aid=a.id ) count 
								FROM '.FDB::table("activities").' as a  ORDER BY count DESC limit 10');
			for( $i=0; $i<count($acti_list); $i++ ){
				$act_temp = unserialize($acti_list[$i]['pics'] );
				$acti_list[$i]['pics'] = $act_temp[0];
				$info_temp = unserialize($acti_list[$i]['info'] );
				$acti_list[$i]['info'] = $info_temp;
			}
	
			
			
			include template('page/event/event_list_myjoin');
			display();
		
		}elseif( substr($type, 0, 6)=="u_new_" ){
		
			$user_id = substr($type, 6);
			
			$user_info = FDB::fetchFirst('SELECT uid,user_name FROM '.FDB::table("user") . ' where uid = '.$user_id );
								
			//用户发起的旅途
			$my_list = array();
			$noew = time();
			$my_list = FDB::fetchAll('select * from '.FDB::table("activities").' where uid='.$user_id .' order by time_start desc ');
			for( $i=0; $i<count($my_list); $i++ ){
				$act_temp = unserialize($my_list[$i]['pics'] );
				$my_list[$i]['pics'] = $act_temp[0];
				$info_temp = unserialize($my_list[$i]['info'] );
				$my_list[$i]['info'] = $info_temp;
			}
			

			//其它活动
			$acti_list = array();
			$acti_list = FDB::fetchAll('SELECT a.*, (select count(id) from '.FDB::table("activities_relation").' where aid=a.id ) count 
								FROM '.FDB::table("activities").' as a  ORDER BY count DESC limit 10');
			for( $i=0; $i<count($acti_list); $i++ ){
				$act_temp = unserialize($acti_list[$i]['pics'] );
				$acti_list[$i]['pics'] = $act_temp[0];
				$info_temp = unserialize($acti_list[$i]['info'] );
				$acti_list[$i]['info'] = $info_temp;
			}
			
			$user_info_title = '<a href="'.FU("u/index", array("uid"=>$user_id)).'">'.$user_info['user_name'].'</a> 的旅途<sup style="font-size:12px;">['.count($my_list).']</sup>&nbsp;&nbsp;&nbsp;&nbsp;
								<span style="font-size:14px;"><a href="'. FU("event/list", array('type'=> 'u_join_'.$user_id )) . '">查看 
								<b>'. $user_info['user_name'] .'</b> 参与的旅途</a></span>' ;
			
		
			include template('page/event/event_list_user');
			display();

		}elseif( substr($type, 0, 7)=="u_join_" ){
		
			$user_id = substr($type, 7);
			
			$user_info = FDB::fetchFirst('SELECT uid,user_name FROM '.FDB::table("user") . ' where uid = '.$user_id );
			
			
			//用户发起的旅途
			$my_list = array();
			$noew = time();
			$my_list = FDB::fetchAll('select * from '.FDB::table("activities").' where id in (select aid from '.FDB::table("activities_relation").'  where uid='.$user_id .') order by time_start desc' );
			for( $i=0; $i<count($my_list); $i++ ){
				$act_temp = unserialize($my_list[$i]['pics'] );
				$my_list[$i]['pics'] = $act_temp[0];
				$info_temp = unserialize($my_list[$i]['info'] );
				$my_list[$i]['info'] = $info_temp;
			}
			

			//其它活动
			$acti_list = array();
			$acti_list = FDB::fetchAll('SELECT a.*, (select count(id) from '.FDB::table("activities_relation").' where aid=a.id ) count 
								FROM '.FDB::table("activities").' as a  ORDER BY count DESC limit 10');
			for( $i=0; $i<count($acti_list); $i++ ){
				$act_temp = unserialize($acti_list[$i]['pics'] );
				$acti_list[$i]['pics'] = $act_temp[0];
				$info_temp = unserialize($acti_list[$i]['info'] );
				$acti_list[$i]['info'] = $info_temp;
			}
			
		
			$user_info_title = '<a href="'.FU("u/index", array("uid"=>$user_id)).'">'.$user_info['user_name'].'</a> 参与的旅途<sup style="font-size:12px;">['.count($my_list).']</sup>&nbsp;&nbsp;&nbsp;&nbsp;
								<span style="font-size:14px;"><a href="'. FU("event/list", array('type'=> 'u_new_'.$user_id )) . '">查看 
								<b>'. $user_info['user_name'] .'</b> 的旅途</a></span>' ;
								
								
			include template('page/event/event_list_user');
			display();

		}

	}
}
?>
